package com.novellotus.iov.carvice.model;

import android.widget.Button;
import android.widget.EditText;

import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.activity.PerfectRegisterActivity;
import com.novellotus.iov.carvice.retrofit.BaseEntity;
import com.novellotus.iov.carvice.retrofit.BaseObserver;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.retrofit.RxSchedulers;
import com.novellotus.iov.carvice.rxjava.MyObserver;
import com.novellotus.iov.carvice.utils.MyUtils;
import com.novellotus.iov.carvice.utils.MyViewUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by Rich on 2017/8/7.
 */

public class PerfectRegisterModel {

    PerfectRegisterActivity context;
    private Observable<CharSequence> ObservableNickName;
    // private Observable<CharSequence> ObservableEmail;
    private Observable<CharSequence> ObservablePassword;

    public PerfectRegisterModel(PerfectRegisterActivity content) {
        this.context = content;
    }

    /**
     * @param nickName 设置昵称编辑框
     */
    public void setNickName(final EditText nickName) {
        ObservableNickName = RxTextView.textChanges(nickName);
        ObservableNickName.compose(RxSchedulers.<CharSequence>lifecycle(context)).subscribe(new MyObserver<CharSequence>() {
            @Override
            public void onNext(CharSequence value) {
                super.onNext(value);
                if (value.length() >= 2) {
                    nickName.setTextColor(context.getResources().getColor(R.color.text_black));
                } else {
                    nickName.setTextColor(context.getResources().getColor(R.color.textTipsRed));
                }
            }
        });
    }

    /**
     * @param email 设置邮箱编辑框
     */
    public void setEmail(final EditText email) {
//        ObservableEmail = RxTextView.textChanges(email);
//        ObservableEmail.subscribe(new MyObserver<CharSequence>() {
//            @Override
//            public void onNext(CharSequence value) {
//                super.onNext(value);
//                if (MyUtils.isEmail(value.toString())) {
//                    email.setTextColor(context.getResources().getColor(R.color.white));
//                } else {
//                    email.setTextColor(context.getResources().getColor(R.color.textTipsRed));
//                }
//            }
//        });
    }

    /**
     * 设置确认注册按钮
     * "email": "string",
     * "mobile": "string",
     * "nickName": "string",
     * "password": "string
     */
    public void setBtnSure(final Button button, final String mobile, final EditText nickName, final EditText password) {
        RxView.clicks(button).compose(RxSchedulers.lifecycle(context)).throttleFirst(2, TimeUnit.SECONDS).subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {

                JSONObject requestData = new JSONObject();
                try {
                    //  requestData.put("email", email.getText().toString());
                    requestData.put("mobile", mobile);
                    requestData.put("nickName", nickName.getText().toString());
                    requestData.put("password", password.getText().toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), requestData.toString());
                RetrofitFactory.getInstance().register(requestBody)
                        .compose(RxSchedulers.<BaseEntity<Boolean>>compose())
                        .subscribe(new BaseObserver<Boolean>(context) {
                            @Override
                            public void onSuccess(Boolean response) {
                                MyUtils.showToast(context, "注册成功!");
                                MyUtils.clearActivity();
                                context.finish();
                            }

                            @Override
                            public void onFail(int code, String msg) {
                                MyUtils.showToast(context, msg);
                            }

                            @Override
                            public void onError(String msg) {
                                MyUtils.showToast(context, msg);
                            }

                        });
            }
        });

    }

    /**
     * @param password 设置密码编辑框
     */
    public void setPassword(final EditText password) {
        ObservablePassword = RxTextView.textChanges(password);
        ObservablePassword.compose(RxSchedulers.<CharSequence>lifecycle(context)).subscribe(new MyObserver<CharSequence>() {
            @Override
            public void onNext(CharSequence value) {
                super.onNext(value);
                if (value.length() >= 6) {
                    password.setTextColor(context.getResources().getColor(R.color.text_black));
                } else {
                    password.setTextColor(context.getResources().getColor(R.color.textTipsRed));
                }
            }
        });
    }

    /**
     * 设置表单验证规则
     */
    public void setConfirm(final Button btn_sure) {
        Observable.combineLatest(ObservableNickName, ObservablePassword, new BiFunction<CharSequence, CharSequence, Boolean>() {
            @Override
            public Boolean apply(CharSequence nickName, CharSequence password) throws Exception {
                return nickName.length() > 1 && password.length() > 5;
            }
        }).compose(RxSchedulers.<Boolean>lifecycle(context)).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean pass) throws Exception {
                if (pass) {
                    MyViewUtils.setButtonClickable(btn_sure, true);
                } else {
                    MyViewUtils.setButtonClickable(btn_sure, false);
                }
            }
        });
    }

}
